﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Xml;

/// <summary>
/// Utility 的摘要说明
/// </summary>
public class Utility
{

	public Utility()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 获取地图范围（州、县、乡镇）
    /// </summary>
    /// <param name="eid">地图的 EID</param>
    /// <returns>返回地图矩形范围</returns>
    public BaseClass.MapExtent GetMapExtent(string sEid)
    {
        BaseClass.MapExtent mapExtent = new BaseClass.MapExtent();
        OracleConnection oconn = DbConnection.GetOracleConnection();

        try
        {
            oconn.Open();
            OracleCommand ocmd = oconn.CreateCommand();
            ocmd.CommandText = "select * from HONGHE.红河州行政区划索引表 where EID=\'" + sEid + "\'";
            ocmd.CommandType = CommandType.Text;
            OracleDataReader rdr = ocmd.ExecuteReader();
            while (rdr.Read())
            {
                mapExtent.Left = Convert.ToDouble(rdr["ZS_JINGDU"]);
                mapExtent.Top = Convert.ToDouble(rdr["ZS_WEIDU"]);
                mapExtent.Right = Convert.ToDouble(rdr["YX_JINGDU"]);
                mapExtent.Bottom = Convert.ToDouble(rdr["YX_WEIDU"]);
            }
        }
        catch (OracleException e)
        {
            string t = e.StackTrace;
        }
        finally
        {
            oconn.Close();
        }

        return mapExtent;
    }

    /// <summary>
    /// 将 EID 转化为对应的 ArcIMS 应用服务名
    /// </summary>
    /// <param name="eid">EID 的值</param>
    /// <returns>ArcIMS 应用服务名</returns>
    public string Eid2AxlName(string sEid)
    {
        Hashtable hashTable = new Hashtable();
        hashTable.Add("00", "ZHOU");
        hashTable.Add("01", "MILE");
        hashTable.Add("02", "LUXI");
        hashTable.Add("03", "JIANSHUI");
        hashTable.Add("04", "KAIYUAN");
        hashTable.Add("05", "MENGZI");
        hashTable.Add("06", "GEJIU");
        hashTable.Add("07", "SHIPING");
        hashTable.Add("08", "PINGBIAN");
        hashTable.Add("09", "YUANYANG");
        hashTable.Add("10", "HONGHE");
        hashTable.Add("11", "JINPING");
        hashTable.Add("12", "LVCHUN");
        hashTable.Add("13", "HEKOU");

        foreach (DictionaryEntry de in hashTable)
        {
            if ((string)de.Key == sEid)
                return (string)de.Value;
        }

        // 默认返回州地图
        return "ZHOU";
    }

    /// <summary>
    /// 将地区编码 EID 转换成地区名称
    /// </summary>
    /// <param name="sEid">EID</param>
    /// <param name="sTable">数据表名，确定是哪级行政单位</param>
    /// <returns>地区名称</returns>
    public string Eid2Name(string sEid, string sTable)
    {
        string sName = "";
        OracleConnection oconn = DbConnection.GetOracleConnection();

        try
        {
            oconn.Open();
            OracleCommand ocmd = oconn.CreateCommand();
            ocmd.CommandText = "select * from " + sTable + " where CODE=\'" + sEid + "\'";
            ocmd.CommandType = CommandType.Text;
            OracleDataReader rdr = ocmd.ExecuteReader();
            while (rdr.Read())
            {
                sName = (string)rdr["NAME"];
            }
        }
        catch (OracleException e)
        {
            string t = e.StackTrace;
        }
        finally
        {
            oconn.Close();
        }

        return sName;
    }

    /// <summary>
    /// 将字典 ID 转换成名称
    /// </summary>
    /// <param name="sEid">ID</param>
    /// <param name="sTable">数据表名</param>
    /// <returns>名称</returns>
    public string Id2Name(string sId, string sTable)
    {
        string sName = "";
        OracleConnection oconn = DbConnection.GetOracleConnection();

        try
        {
            oconn.Open();
            OracleCommand ocmd = oconn.CreateCommand();
            ocmd.CommandText = "select * from " + sTable + " where ID=\'" + sId + "\'";
            ocmd.CommandType = CommandType.Text;
            OracleDataReader rdr = ocmd.ExecuteReader();
            while (rdr.Read())
            {
                sName = (string)rdr["NAME"];
            }
        }
        catch (OracleException e)
        {
            string t = e.StackTrace;
        }
        finally
        {
            oconn.Close();
        }

        return sName;
    }

    /// <summary>
    /// 在服务器响应的 ArcXML 文件中根据图层名查询图层 id
    /// <remarks>
    /// 暂时没有使用
    /// </remarks>
    /// </summary>
    /// <param name="sResponse">服务器响应的 ArcXML 文件</param>
    /// <param name="sLayerName">图层名</param>
    /// <returns>图层 id</returns>
    public string LayerName2Id(string sResponse, string sLayerName)
    {
        string sLayerId = "";

        XmlDocument myDoc = new XmlDocument();
        myDoc.LoadXml(sResponse);
        XmlElement root = myDoc.DocumentElement;
        XmlNodeList myNodeList = root.GetElementsByTagName("LAYERINFO");

        for (int i = 0; i < myNodeList.Count; i++)
        {
            if (myNodeList[i].Attributes["name"].InnerXml.ToLower() == sLayerName.ToLower())
            {
                sLayerId = myNodeList[i].Attributes["id"].InnerXml;
                break;
            }
        }
        return sLayerId;
    }

    static public string hexToRGB(string hex)
    {
        Hashtable hexTable = new Hashtable();
        hexTable.Add("0", "0");
        hexTable.Add("1", "1");
        hexTable.Add("2", "2");
        hexTable.Add("3", "3");
        hexTable.Add("4", "4");
        hexTable.Add("5", "5");
        hexTable.Add("6", "6");
        hexTable.Add("7", "7");
        hexTable.Add("8", "8");
        hexTable.Add("9", "9");
        hexTable.Add("a", "10");
        hexTable.Add("b", "11");
        hexTable.Add("c", "12");
        hexTable.Add("d", "13");
        hexTable.Add("e", "14");
        hexTable.Add("f", "15");
        hex = hex.ToLower();
        if (hex.StartsWith("#"))
            hex = hex.Remove(0, 1);


        string hexR = hex.Substring(0, 2);
        string hexG = hex.Substring(2, 2);
        string hexB = hex.Substring(4, 2);

        string rgbR = (Convert.ToInt32(hexTable[hexR.Substring(0, 1)]) * 16 + Convert.ToInt32(hexTable[hexR.Substring(1, 1)])).ToString();
        string rgbG = (Convert.ToInt32(hexTable[hexG.Substring(0, 1)]) * 16 + Convert.ToInt32(hexTable[hexG.Substring(1, 1)])).ToString();
        string rgbB = (Convert.ToInt32(hexTable[hexB.Substring(0, 1)]) * 16 + Convert.ToInt32(hexTable[hexB.Substring(1, 1)])).ToString();
        return rgbR + "," + rgbG + "," + rgbB;
    }

}
